<template>
  <el-dialog
    v-dialogDrag
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="组编码" prop="dictGroup">
            <el-select v-model="dataForm.dictGroup" placeholder="请选择组编码" style="width: 100%">
              <template v-for="(item, index) in dict.AreaGroups">
                <el-option :key="index" :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="级联父ID" prop="dictPid">
            <el-input v-model="dataForm.dictPid" placeholder="级联父ID" class="rdonly" :readonly="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="dictLabel">
            <el-input v-model="dataForm.dictLabel" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="数据值" prop="dictValue">
            <el-input v-model="dataForm.dictValue" placeholder="数据值"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="样式" prop="dictStyle">
            <el-select v-model="dataForm.dictStyle" placeholder="请选择样式" style="width: 100%">
              <el-option label="primary:主要" value="primary"></el-option>
              <el-option label="success:成功" value="success"></el-option>
              <el-option label="info:信息" value="info"></el-option>
              <el-option label="warning:警告" value="warning"></el-option>
              <el-option label="danger:危险" value="danger"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否默认选项" prop="dictDefault">
            <el-select v-model="dataForm.dictDefault" placeholder="请选择" style="width: 100%">
              <template v-for="(item, index) in dict.YN">
                <el-option :key="index" :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
            <!--<el-switch
              v-model="dataForm.dictDefault"
              active-color="#13ce66"
              inactive-color="#d3d3d3"
              active-value="1"
              inactive-value="0">
            </el-switch>-->
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="描述" prop="dictDesc">
        <el-input v-model="dataForm.dictDesc" placeholder="描述"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="顺序号" prop="orders">
            <el-input v-model="dataForm.orders" placeholder="顺序号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="请选择状态" style="width: 100%">
              <template v-for="(item, index) in dict.Status">
                <el-option :key="index" :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!--<el-form-item label="启用状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="启用状态"></el-input>
      </el-form-item>
      <el-form-item label="可维护标识" prop="editLevel">
        <el-input v-model="dataForm.editLevel" placeholder="可维护标识"></el-input>
      </el-form-item>
      <el-form-item label="版本号" prop="version">
        <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
      </el-form-item>
      <el-form-item label="时间戳" prop="timeStamp">
        <el-input v-model="dataForm.timeStamp" placeholder="时间戳"></el-input>
      </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dictGroup: '',
          dictPid: '',
          dictValue: '',
          dictLabel: '',
          dictDesc: '',
          orders: '',
          status: '',
          dictStyle: '',
          dictDefault: '',
        },
        dataRule: {
          dictGroup: [
            {required: true, message: '组编码不能为空', trigger: 'blur'}
          ],
          dictPid: [
            {required: true, message: '级联父ID不能为空', trigger: 'blur'}
          ],
          dictValue: [
            {required: true, message: '数据值不能为空', trigger: 'blur'}
          ],
          dictLabel: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ],
          orders: [
            {required: true, message: '顺序号不能为空', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '状态不能为空', trigger: 'blur'}
          ],
          dictStyle: [
            { message: '样式不能为空', trigger: 'blur'}
          ],
          dictDefault: [
            {required: true, message: '是否默认不能为空', trigger: 'blur'}
          ],
        }
      }
    },
    methods: {
      init(id, row) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.dataForm.dictPid = 0;
          if (this.dataForm.id) {
            this.dataForm = row
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/api/dictionary/${!this.dataForm.id ? 'add' : 'mod'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
